package com.music.checkpoints.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.music.checkpoints.common.amqp.AmqpExchange;
import com.music.checkpoints.common.convert.NationQuestion2NationQuestionVO;
import com.music.checkpoints.common.convert.Noanswer2NoanswerVO;
import com.music.checkpoints.common.dao.*;
import com.music.checkpoints.common.dto.NationExamDetailDTO;
import com.music.checkpoints.common.dto.NoanswerDTO;
import com.music.checkpoints.common.entity.NationExamDetail;
import com.music.checkpoints.common.entity.NationNoanswerTempory;
import com.music.checkpoints.common.entity.NationSignUp;
import com.music.checkpoints.common.enums.ErrorCodeEnum;
import com.music.checkpoints.common.exception.ServiceException;
import com.music.checkpoints.common.service.QuestionManager;
import com.music.checkpoints.common.vo.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionManagerImpl implements QuestionManager {
    @Autowired
    private NationSignUpDao nationSignUpDao;
    @Autowired
    private NationQuestionDao nationQuestionDao;
    @Autowired
    private NationExamDetailDao nationExamDetailDao;
    @Autowired
    private NationNoanswerDao nationNoanswerDao;
    @Autowired
    private NationNoanswerTemporyDao noanswerTemporyDao;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<NationQuestionVO> questions(String examCode) {
        NationSignUp nationSignUp = nationSignUpDao.querySignUpByExamCode(examCode);
        if (nationSignUp == null) {
            throw new ServiceException(ErrorCodeEnum.E001.code(), ErrorCodeEnum.E001.getDescription());
        }
        if (examCode.equals("123456") || examCode.equals("20200001") || examCode.equals("20200002") || examCode.equals("20200003")) {
            return nationQuestionDao.queryTestByLevel(nationSignUp.getLevel())
                    .stream().map(e -> NationQuestion2NationQuestionVO.convert(e))
                    .collect(Collectors.toList());
        } else {
            return nationQuestionDao.queryQuestionsByLevel(nationSignUp.getLevel())
                    .stream().map(e -> NationQuestion2NationQuestionVO.convert(e))
                    .collect(Collectors.toList());
        }
    }

    @Override
    public void submitAnswer(DetailVO detailVO) {
        NationSignUp nationSignUp = nationSignUpDao.querySignUpByExamCode(detailVO.getExamCode());
        if (nationSignUp == null) {
            throw new ServiceException(ErrorCodeEnum.E001.code(), ErrorCodeEnum.E001.getDescription());
        }
        NationExamDetail nationExamDetail = nationExamDetailDao.queryByExamCode(detailVO.getExamCode());
        if (nationExamDetail != null) {
            throw new ServiceException(ErrorCodeEnum.E004.code(), ErrorCodeEnum.E004.getDescription());
        }

        String jsonMsg = JSON.toJSONString(detailVO);
        amqpTemplate.convertAndSend(AmqpExchange.SUBMIT_ANSWER,
                AmqpExchange.SUBMIT_ANSWER + "_ROUTING",
                jsonMsg);
        return;
    }

    @Override
    public void saveAnswerDetail(DetailVO detailVO) {
        NationSignUp nationSignUp = nationSignUpDao.querySignUpByExamCode(detailVO.getExamCode());
        NationExamDetailDTO detailDTO = new NationExamDetailDTO();
        detailDTO.setUserId(nationSignUp.getUid());
        detailDTO.setExamCode(nationSignUp.getExamCode());
        detailDTO.setLevel(nationSignUp.getLevel());
        detailDTO.setType(1);
        detailDTO.setScore(detailVO.getScore());
        detailDTO.setDetail(JSON.toJSONString(detailVO.getAnswer()));
        detailDTO.setCreatedAt(new Timestamp(new Date().getTime()));
        detailDTO.setUpdatedAt(new Timestamp(new Date().getTime()));
        nationExamDetailDao.insertExamDetail(detailDTO);
        return;
    }

    @Override
    public List<NoanswerVO> subjectQuestions(String code, Integer type) {
        NationSignUp nationSignUp = nationSignUpDao.querySignUpByExamCode(code);
        if (nationSignUp == null) {
            throw new ServiceException(ErrorCodeEnum.E001.code(), ErrorCodeEnum.E001.getDescription());
        }
        Integer answerType = type == 1 ? 7 : 8;
        NoanswerDTO noanswerDTO = new NoanswerDTO();
        noanswerDTO.setAnswerType(answerType);
        noanswerDTO.setLevel(nationSignUp.getLevel());
        if (code.equals("123456") || code.equals("20200001") || code.equals("20200002") || code.equals("20200003")) {
            noanswerDTO.setLimit(1);
        } else {
            noanswerDTO.setLimit(20);
        }
        return nationNoanswerDao.questions(noanswerDTO).stream().map(e -> Noanswer2NoanswerVO.convert(e)).collect(Collectors.toList());
    }

    @Override
    public void submitSubject(SubjectVO subjectVO) {
        NationSignUp nationSignUp = nationSignUpDao.querySignUpByExamCode(subjectVO.getExamCode());
        if (nationSignUp == null) {
            throw new ServiceException(ErrorCodeEnum.E001.code(), ErrorCodeEnum.E001.getDescription());
        }
        if (subjectVO.getSubject().size() > 0) {
            for (SubjectQuestVO questVO : subjectVO.getSubject()) {
                NationNoanswerTempory tempory = new NationNoanswerTempory();
                tempory.setUid(nationSignUp.getUid());
                tempory.setSiteId(nationSignUp.getExamId());
                tempory.setExamCode(subjectVO.getExamCode());
                tempory.setQuestionId(questVO.getQuestionId());
                tempory.setAnswerType(questVO.getAnswerType());
                tempory.setSingPath(questVO.getSingPath());
                tempory.setQuestionUrl(questVO.getQuestionUrl());
                tempory.setCreatedAt(new Timestamp(new Date().getTime()));
                tempory.setUpdatedAt(new Timestamp(new Date().getTime()));
                noanswerTemporyDao.initNoanswerTempory(tempory);
            }
        }
        return;
    }
}
